/**
 * 删除二叉搜索树中的节点
 * 
 * 给定一个二叉搜索树的根节点 root 和一个值 key，删除二叉搜索树中的 key 对应的节点，并保证二叉搜索树的性质不变。
 * 返回二叉搜索树（有可能被更新）的根节点的引用。
 */

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} key
 * @return {TreeNode}
 */

var getSuccessor = (delNode) => {
    let successor = delNode,
        successorParent = delNode,
        current = delNode.right
    
    while(current) {
        successorParent = successor
        successor = current
        current = current.left
    }    

    if (successor !== delNode.right) {
        successorParent.left = successor.right
        successor.right = delNode.right
    }
    return successor
}
var deleteNode = function(root, key) {
    /**
     * 思路：
     * 1、找到要删除的节点
     * 2、如果没有找到，返回 null
     * 3、如果找到了，判断左右子树
     *   3.1 如果左右子树都为空，直接删除
     *   3.2 如果左子树为空，右子树顶替
     *   3.3 如果右子树为空，左子树顶替
     *   3.4 如果左右子树都不为空，找到右子树的最小节点，替换当前节点（即找后继节点或者前驱节点Z）
     * 
     * 找后继节点：
     *  1、找到右子树的最小节点，即右子树的最左节点
     *  2、替换当前节点
     */
    let current = root, parent = null, isLeft = false

    while(current && current.val !== key) {
        parent = current
        if (key < current.val) {
            current = current.left
            isLeft = true
        } else if (key > current.val) {
            current = current.right
            isLeft = false
        }
    }

    if (current === null) return root
    if (current.left === null && current.right === null) {
        if (current ===  root) {
            return null
        } else if (isLeft) {
            parent.left = null
        } else {
            parent.right = null
        }
    } else if (current.left === null) {
        if (current === root) {
            root = current.right
        } else if (isLeft) {
            parent.left = current.right
        } else {
            parent.right = current.right
        }
    } else if (current.right === null) {
        if (current === root) {
            root = current.left
        } else if (isLeft) {
            parent.left = current.left
        } else {
            parent.right = current.right
        }
    } else {
        let successor = getSuccessor(current)
        if (current === root) {
            root = successor
        } else if (isLeft) {
            parent.left = successor
        } else {
            parent.right = successor
        }
        successor.left = current.left

    }
    return root
};


/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} key
 * @return {TreeNode}
 */

/**
 * 
 * 递归的使用原理：
 * 1、递归的终止条件：当root为空的时候，直接返回null
 * 2、递归的返回值：返回的是当前root的左右子树
 * 3、递归的参数：当前root节点和要删除的key
 * 
 */
var deleteNode = function (root, key) {
    // 如果没有root直接返回null
    if (root === null) return null;
    // 如果查到了root.val等于key，那么就检查是否有左右子树
    // 如果没有左子树，说明是单一右子树，则返回右子树的孩子即可，反之亦然
    // 如果左右子树都有，则找当前root跟的后继，找到后继后删除，将后继的值赋值给根的值
    if (root.val === key) {
        if (root.left === null) return root.right;
        if (root.right === null) return root.left;
        let next = root.right;
        while (next.left) next = next.left;
        root.right = deleteNode(root.right, next.val);
        root.val = next.val;
        return root;
    }
    // 如果当前跟的值大于key，则向左子树删除查找，反之亦然
    if (root.val > key) {
        root.left = deleteNode(root.left, key);
    } else {
        root.right = deleteNode(root.right, key);
    }
    return root;
};
